<?php
namespace app\index\controller;


use app\index\model\Descartes;
use jianyan\excel\Excel;
use Phpml\Classification\NaiveBayes;
use Phpml\ModelManager;
use think\facade\Cache;
use think\facade\Db;

class Index
{
    public function index(){
       echo '这里是index';
    }
    public function handlexlsx(){
        $data = Excel::import('nursing_lang.xlsx');
        unset($data[0]);
        $data = array_values($data);
//        echo '<pre>';
//        print_r($data);
//        exit;
        $newdata = [];
        foreach ($data as $key=>$value){
            if($key>4){
                continue;
            }
            unset($value[0]);
            unset($value[6]);
            $value = array_values($value);
            $n_value = [];
            foreach ($value as $k=>$v){
                if($k>9){
                    continue;
                }
                if($k==9){
                    $n_value[] = $v;
                }else{
                    $v = explode('/',$v);
                    if(count($v)==1){
                        $v = explode('、',$v[0]);
                    }
                    $n_value[] = $v;
                }
            }
            $newdata[] = $n_value;
        }
        $y_newdata[0] = $newdata[4];
        $y_newdata[1] = $newdata[1];
        $y_newdata[2] = $newdata[1];
        $y_newdata[3] = $newdata[1];
        $y_newdata[4] = $newdata[1];
        echo '<pre>';
        print_r($newdata);
        exit;
    }
    public function indexs(){
        $data = Excel::import('附表4：长期照护服务内容.xlsx');
//        echo '<pre>';
//        print_r($data);
//        exit;
        $newdata = [];
        foreach ($data as $key=>$value){
            if(empty($value[0])){
                continue;
            }
            $n_va = [];
            foreach ($value as $ke=>$va){
                if($ke>1){
                    continue;
                }
                if($ke==1){
                    if(!empty($va)){
                        $va = explode('|',$va);
                    }
                }
                $n_va[] = $va;
            }
            $newdata[] = $n_va;
        }
        $nursing_lang_id = '';
//        echo '<pre>';
//        print_r($newdata);
//        exit;
        $newkey = 0;
        foreach ($newdata as $key=>$value){
            if(empty($value[1])){
                $name = trim($value[0]);
                $nursing_lang = Db::name('nursing_lang')
                    ->where('name',$name)
                    ->find();
                $nursing_lang_id = $nursing_lang['id'];
                $newkey = 100;
            }else{
//                $pid = 0;
                $getid = Db::name('service_content')
                    ->insertGetId([
                        'name'=>trim($value[0]),
                        'sort'=>$newkey,
                        'pid'=>0,
                        'nursing_lang_id'=>$nursing_lang_id,
                        'status'=>1,
                        'create_time'=>time(),
                        'update_time'=>time(),
                    ]);
                foreach ($value[1] as $kes=>$ves){
                    Db::name('service_content')
                        ->insert([
                            'name'=>trim($ves),
                            'sort'=>100-$kes,
                            'pid'=>$getid,
                            'nursing_lang_id'=>$nursing_lang_id,
                            'status'=>1,
                            'create_time'=>time(),
                            'update_time'=>time(),
                        ]);
                }

            }
        }
        echo '完成';
    }

}